﻿using System.Collections.Generic;
using System.Diagnostics;
using CSRedis;
using Manage.Traceability.Api.DTO;
using Manage.Traceability.Domain;
using Manage.Traceability.ErrorCode;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace Manage.Traceability.Api.Controllers
{
    /// <summary>
    /// 溯源控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SourceController : ControllerBase
    {
        private readonly ISqlSugarClient db;

        public SourceController(ISqlSugarClient db)
        {
            this.db = db;
        }
        /// <summary>
        /// 显示商品
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<List<ShopModels>> GetShop()
        {
            var key = "hello";
            var redisList = RedisHelper.Get<List<ShopModels>>(key);
            if(redisList==null)

            {
                List<ShopModels> list = db.Queryable<ShopModels>().ToList();
                RedisHelper.Set(key, list,60);
                RedisHelper.HSet("StudentHash", key, list);
                return ApiResult<List<ShopModels>>.Success("查询成功", list);
            }
            return ApiResult<List<ShopModels>>.Success("查询成功", redisList);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sourceModels"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> AddSource(SourceModels sourceModels)
        {
            int r1= db.Insertable(sourceModels).ExecuteCommand(); //都是参数化实现
            if(r1>0)
            {
                return ApiResult<int>.Success("添加成功", r1);
            }
            else
            {
                return ApiResult<int>.Fail("添加失败");
            }
        }
        /// <summary>
        /// 显示溯源(分页)
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<ApiPages<SourceDTO>>ShowSource([FromQuery]int pageindex,int pagesize)
        {
            var query5 = db.Queryable<SourceModels>()
             .LeftJoin<ShopModels>((a, b) => a.ShopId == b.ShopId)//多个条件用&&
             .Where(a => a.ShopIsdelete == false)
             .Select((a, b) => new SourceDTO
             {
                 SourceId = a.SourceId,
                 ShopId = a.ShopId,
                 ShopName = b.ShopName,
                 ShopBatch = a.ShopBatch,
                 ShopBestDate = a.ShopBestDate,
                 Shoppermit = a.Shoppermit,
                 ShopFirm = a.ShopFirm,
                 ShopPletene = a.ShopPletene,
                 ShopImg=a.ShopImg,
             });
            int pageindexs = pageindex; // pagenumber是从1开始的不是从零开始的
            int pagesizes = pagesize;
            int listcount = query5.Count();
            query5 = query5.OrderBy(a => a.SourceId);
            //单表分页
            var page = query5.ToPageList(pageindexs, pagesizes, ref listcount);
            ApiPages<SourceDTO> pages = new ApiPages<SourceDTO>()
            {
                ListCount = listcount,
                Data = page
            };
            return ApiResult<ApiPages<SourceDTO>>.Success("显示成功", pages);
        }
        /// <summary>
        /// 删除溯源
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public ApiResult<int> DelSource(int id)
        {

            int r1 = db.Deleteable<SourceModels>().In(id).ExecuteCommand();
            if(r1>0)
            {
                return ApiResult<int>.Success("删除成功",r1);
            }
            else
            {
                return ApiResult<int>.Fail("删除失败");
            }
        }
        /// <summary>
        /// 修改溯源
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<int> UpdateSource(SourceModels source)
        {
            //根据主键更新单条 参数 Class
            int result = db.Updateable(source).Where(x=>x.SourceId== source.SourceId).ExecuteCommand();//实体有多少列更新多少列
            if(result>0)
            {
                return ApiResult<int>.Success("修改成功",result);
            }
            else
            {
                return ApiResult<int>.Fail("修改失败");
            }
        }
        //public IActionResult UpLoadImg(IFormFile formFile)
        //{
        //    var filename=formFile.FileName;
        //    var length=formFile.Length;
        //    var ext=Path.GetExtension(filename);
        //    if(ext)
        //    {

        //    }
        //    if(length>2*1024*1024)
        //    {
        //        return BadRequest("图片大小不能超过2M");
        //    }
        //    var newFileName=Guid.NewGuid().ToString()+ext;
        //    var dirYearParh=DateTime.Now.Year;
        //    var dirPath=Directory.GetCurrentDirectory()+"//";
        //}
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpDelete]
        public ApiResult<int>DelAll(string ids)
        {
            var idslist=ids.Split(',').Select(x=>Convert.ToInt32(x));
            int r1 = db.Deleteable<SourceModels>().In(idslist).ExecuteCommand();
            if (r1 > 0)
            {
                return ApiResult<int>.Success("删除成功", r1);
            }
            else
            {
                return ApiResult<int>.Fail("删除失败");
            }
        }
        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiResult<SourceDTO>DetailSource(int id)
        {
            var query5 = db.Queryable<SourceModels>()
             .LeftJoin<ShopModels>((a, b) => a.ShopId == b.ShopId)//多个条件用&&
             .Where(a => a.ShopIsdelete == false&&a.SourceId==id)
             .Select((a, b) => new SourceDTO
             {
                 SourceId = a.SourceId,
                 ShopId = a.ShopId,
                 ShopName = b.ShopName,
                 ShopBatch = a.ShopBatch,
                 ShopBestDate = a.ShopBestDate,
                 Shoppermit = a.Shoppermit,
                 ShopFirm = a.ShopFirm,
                 ShopPletene = a.ShopPletene,
                 ShopImg = a.ShopImg,
             }).ToList().First();
            return ApiResult<SourceDTO>.Success("显示成功", query5);
        }
    }
    public class ABC
    {
        public delegate void MyDelegete();
        public delegate int MyDelegeteA(int a,int b);
        public delegate string MyDelegeteC(string c);
        public void MethodA()
        {
            Console.WriteLine("MethodA被调用");
        }
        public void MetrhodB()
        {
            Console.WriteLine("MethodB被调用");
        }
        public int MethodC(int a, int b)
        {
            return a + b;
        }
        public void Main()
        {
            //内置委托 Action Func
            //有参数,无返回值 Action
            Action<int, string, bool> action = (a, b, c) => { };
            Action action1 = () => { };
            //有参数,有返回值
            Func<int> func = () => { return 1; };
            Func<int, string> func1 = (a) =>
            {
                return a.ToString();
            };
            //定义(委托类型--(引用类型))
            MyDelegete mydelete = MethodA;
            mydelete();
            //委托指向了一个匿名方法
            MyDelegete mydelegateTest = () =>
            {
                Console.WriteLine("Mythods被调用");
            };
            MyDelegeteA mydelegateTest1 = (a, b) =>
            {
                return 2;
            };
            MyDelegeteC myDelegeteC = (c) =>
            {
                return "你是谁";
            };
        }
    }
}